package com.gzz.orchid.common.core.util;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 数据加解密摘要
 *
 * @author higmd
 */
@Slf4j
public class CryptoUtil {

    /**
     * MD5加密
     */
    public static String md5Encode(String origin) {
        return md5Encode(origin, "UTF-8");
    }

    /**
     * MD5加密
     *
     * @param origin  待加密串
     * @param charset 字符编码 UTF-8,GBK
     */
    public static String md5Encode(String origin, String charset) {
        try {
            byte[] tmps = origin.getBytes(charset);
            return encode(tmps, "MD5");
        } catch (UnsupportedEncodingException e) {
            log.error("转换失败[{}] {}", charset, e.getMessage());
        }
        return null;
    }

    /**
     * SHA加密
     *
     * @param origin 待加密串
     */
    public static String shaEncode(String origin) {
        return shaEncode(origin, "UTF-8");
    }

    /**
     * SHA加密
     *
     * @param origin  待加密串
     * @param charset 字符编码 UTF-8,GBK
     */
    public static String shaEncode(String origin, String charset) {
        try {
            byte[] tmps = origin.getBytes(charset);
            return encode(tmps, "SHA");
        } catch (UnsupportedEncodingException e) {
            log.error("转换失败[{}] {}", charset, e.getMessage());
        }
        return null;
    }

    /**
     * SHA256加密
     *
     * @param origin 待加密串
     */
    public static String sha256Encode(String origin) {
        return sha256Encode(origin, "UTF-8");
    }

    /**
     * SHA256加密
     *
     * @param origin  待加密码串
     * @param charset 字符编码 UTF-8,GBK
     */
    public static String sha256Encode(String origin, String charset) {
        try {
            byte[] tmps = origin.getBytes(charset);
            return encode(tmps, "SHA-256");
        } catch (UnsupportedEncodingException e) {
            log.error("转换失败[{}] {}", charset, e.getMessage());
        }
        return null;
    }

    /**
     * AES加密
     *
     * @param origin 待加密码串
     */
    public static String aesEncode(String origin) {
        return sha256Encode(origin, "UTF-8");
    }

    /**
     * AES加密
     *
     * @param origin  待加密码串
     * @param charset 字符编码 UTF-8,GBK
     */
    public static String aesEncode(String origin, String charset) {
        try {
            byte[] tmps = origin.getBytes(charset);
            return encode(tmps, "AES");
        } catch (UnsupportedEncodingException e) {
            log.error("转换失败[{}] {}", charset, e.getMessage());
        }
        return null;
    }

    /**
     * 加密， 生成一个消息摘要（）
     *
     * @param origin    待加密串
     * @param algorithm 加密方式 MD5/SHA-1/DSA/DESede/AES/DES/Diffie-Hellman
     */
    public static String encode(byte[] origin, String algorithm) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance(algorithm);
            //return StringUtil.ByteToHex(md.digest(origin));
            md.reset();
            md.update(origin);
            return StringUtil.byteToHex(md.digest());
        } catch (NoSuchAlgorithmException e) {
            log.error("加密失败[{}] {}", algorithm, e.getMessage());
        }

        return null;
    }

    /**
     * URL加密成URL
     */
    public static String urlEncodeUtf8(String source) {
        return java.net.URLEncoder.encode(source, StandardCharsets.UTF_8);
    }

}
